package com.ruoyi.ldzlsc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.ldzlsc.service.ScProductInventoryService;
import com.ruoyi.system.api.domain.BasicProduct;

/**
 * 产品库存Controller
 *
 * @author ldzl
 * @date 2025-05-28
 */
@RestController
@RequestMapping("/sc/product")
public class ScProductInventoryController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(ScProductInventoryController.class);
    
    @Autowired
    private ScProductInventoryService scProductInventoryService;

    /**
     * 查询产品列表
     */
    @PostMapping("/list")
    public TableDataInfo list(BasicProduct basicProduct) {
        startPage();
        return scProductInventoryService.listAllProducts(basicProduct);
    }

    /**
     * 查询产品库存状态
     */
    @PostMapping("/stockStatus")
    public AjaxResult stockStatus(@RequestBody List<Long> productIds) {
        log.info("查询产品库存状态，productIds: {}", productIds);
        if (productIds == null || productIds.isEmpty()) {
            log.warn("productIds为空或为空列表");
            return AjaxResult.error("产品ID列表不能为空");
        }
        
        // 输出每个产品ID的详细信息
        for (int i = 0; i < productIds.size(); i++) {
            Long id = productIds.get(i);
            log.info("产品ID[{}]: {}, 类型: {}", i, id, id == null ? "null" : id.getClass().getName());
        }
        
        // 过滤掉null值
        List<Long> filteredIds = productIds.stream()
            .filter(id -> id != null)
            .collect(Collectors.toList());
        
        if (filteredIds.isEmpty()) {
            log.warn("过滤后的productIds为空列表");
            return AjaxResult.error("有效产品ID列表不能为空");
        }
        
        log.info("过滤后的有效productIds: {}", filteredIds);
        List<Map<String, Object>> result = scProductInventoryService.getProductStockStatus(filteredIds);
        return AjaxResult.success(result);
    }

    /**
     * 检查产品是否可以直接出库
     */
    @PostMapping("/canOutbound")
    public AjaxResult canOutbound(@RequestBody List<Long> productIds) {
        log.info("检查产品是否可以直接出库，productIds: {}", productIds);
        if (productIds == null || productIds.isEmpty()) {
            log.warn("productIds为空或为空列表");
            return AjaxResult.error("产品ID列表不能为空");
        }
        boolean canOutbound = scProductInventoryService.canProductOutbound(productIds);
        Map<String, Object> result = new HashMap<>();
        result.put("canOutbound", canOutbound);
        return AjaxResult.success(result);
    }

    /**
     * 检查产品是否需要生产
     */
    @PostMapping("/needProduction")
    public AjaxResult needProduction(@RequestBody List<Long> productIds) {
        log.info("检查产品是否需要生产，productIds: {}", productIds);
        if (productIds == null || productIds.isEmpty()) {
            log.warn("productIds为空或为空列表");
            return AjaxResult.error("产品ID列表不能为空");
        }
        boolean needProduction = scProductInventoryService.needProductProduction(productIds);
        Map<String, Object> result = new HashMap<>();
        result.put("needProduction", needProduction);
        return AjaxResult.success(result);
    }

    // 自动生成编号
    @GetMapping("/auto/{enCode}")
    public AjaxResult automaticallyNumbers(@PathVariable Long enCode) {
        return scProductInventoryService.automaticallyNumbers(enCode);
    }
} 